/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package net.qunzhi.common.utils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 日期处理
 *
 */
public class LocalDateUtils {
	/** 时间格式(yyyy-MM-dd) */
	public final static String DATE_PATTERN = "yyyy-MM-dd";
	public final static String DATE_PATTERN_BAR = "yyyy/MM/dd";
	/** 时间格式(yyyy-MM-dd HH:mm:ss) */
	public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    /**
     * 日期格式化 日期格式为：yyyy-MM-dd
     * @param date  日期
     * @return  返回pattern格式日期
     */
    public static String dateFormat(LocalDate date, String pattern) {
        return format(date, pattern);
    }

    public static String format(LocalDate date, String pattern) {
        if(date != null){
            var dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
            return dateTimeFormatter.format(date);
        }
        return null;
    }

    public static String dateFormat(LocalDateTime date, String pattern) {
        return format(date, pattern);
    }

    public static String format(LocalDateTime date, String pattern) {
        if(date != null){
            var dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
            return dateTimeFormatter.format(date);
        }
        return null;
    }

    public static LocalDateTime stringToDateTime(String dateTime) {

        return LocalDateTime.parse(dateTime.substring(0,19), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }
}
